草庐IT

Android JSONObject 与 GSON

全部标签

android - Gson 和 Active Android : Attempted to serialize java. lang.Class。忘记注册类型适配器?

我正在使用Gson序列化一个ActiveAndroid模型。模型类只包含原语,Gson使用默认设置序列化它应该没有问题。但是,当我尝试时,我得到了错误:java.lang.UnsupportedOperationException:Attemptedtoserializejava.lang.Class:.Forgottoregisteratypeadapter?我真的不想为我的每个模型类编写一个类型适配器,我该如何解决这个问题? 最佳答案 想通了。当然,ActiveAndroid的基础模型类是添加了默认不能序列化的字段。可以使用Gs

android - 使用 Retrofit 和 Gson 解析 JSON 数组响应

这是来自Web服务的JSONArray响应:[{"sponsors":[{"leg_id":"NYL000067","type":"primary","name":"AUBRY"},{"leg_id":"NYL000171","type":"cosponsor","name":"PERRY"},{"leg_id":"NYL000066","type":"cosponsor","name":"ARROYO"},{"leg_id":"NYL000223","type":"cosponsor","name":"BARRETT"},{"leg_id":"NYL000312","type":"c

java - 使用 Gson 时出现奇怪的 "nameValuePairs"键

我正在尝试从其字段重建一个Object(我将字段作为JSONObject获取),如下所示:JSONObjectjObj=newJSONObject();JSONObjectjObj1=newJSONObject();JSONObjectjObj2=newJSONObject();JSONObjectjObj21=newJSONObject();JSONObjectjObj22=newJSONObject();jObj1.put("jObj11","value11");jObj1.put("jObj12","value12");jObj21.put("jObj211","value211

android - Gson在发布的apk中反序列化空指针

我正在编写一个需要使用gson反序列化json字符串的Android应用:{"reply_code":001,"userinfo":{"username":"002","userip":003}}所以我创建了两个类:publicclassReturnData{publicStringreply_code;publicuserinfouserinfo;}publicclassuserinfo{publicStringusername;publicStringuserip;}最后,我在MainActivity.java中的Java代码:@OverrideprotectedvoidonCre

android - 使用启用了 proguard 的 GSON

我的代码在没有proguard的情况下可以正常工作,但是在启用proguard时GSON不起作用。这是代码中不起作用的部分JSONArraymensaje=response.getJSONArray("categorias");//ParsearconGsonCategorias[]categorias=gson.fromJson(mensaje.toString(),Categorias[].class);Log.d("mainfragment","desccategoria"+categorias[0].getDescripcionCategoria());mainfragment

android - 为什么android找不到com.google.gson.Gson

我试图在我的项目中使用GSON,但我的应用程序崩溃了,logcat说找不到com.google.gson.Gson。我已将importcom.google.gson.Gson放在我的类文件中,我的包资源管理器中有gson,并通过右键单击->构建路径->添加库来添加它。它还显示在Project->properties->javabuildpath->librariestab->gson中。我做错了什么? 最佳答案 什么对我有用:选中lib(gson-2.0.jar)旁边的复选框:“项目属性”->“Java构建路径”->“订购和导出”选

android - 将 ForeignCollection 转换为 ArrayList - ORMLite、Gson 和 Android

如果我的解释不是很清楚,我深表歉意,但如果需要,我会添加并编辑此问题以清楚起见。我正在开发一个Android应用程序,它通过外部API接收数据并使用ORMLite在本地存储数据。在本地存储数据和使用ORMLite之前,我有从服务器检索JSON并通过以下方式解析它的模型:Gsongson=newGson();Stringresult=ApiClient.httpPost("/user_route");Useruser=gson.fromJson(result,User.class);定义了用户类publicclassUser{intid;Stringname;ArrayListmedia

android - GSON 相对于普通 JSON 解析的优势

我正在开发的应用程序主要基于处理从服务器获取的JSON数据。传统的JSON解析器提取值、设置所需的POJO并传递给UI处理程序进行渲染。这部分目前运行良好。我听说过GSON库并完成了它的实现步骤。据我了解,它(GSON用法)需要以下内容。采用正确语法的JSON数据。与JSON响应匹配的模型类对象。GSON注入(inject)器或代码fragment,用于从服务器获取JSON并提供给GSON。上述方法听起来很像对象映射。但是,与老式的JSON解析相比,我不知道GSON的效率如何。特别是对于复杂的JSON。以及它对内存使用的影响?你怎么看? 最佳答案

java - 如何将 JSONObject 转换为 gson.JsonObject?

我有一个org.json.JSONObject对象。从中创建gson.JsonObject对象的最简单方法是什么?谢谢 最佳答案 最简单的方法是使用toString()将JSONObject序列化为json字符串,然后将该json字符串解析为JsonObject:org.json.JSONObjectobject=;JsonParserjsonParser=newJsonParser();JsonObjectgsonObject=(JsonObject)jsonParser.parse(object.toString());请注意,

java - 如何使用 GSON 解析动态 JSON 字段?

所以我使用GSON来解析来自API的JSON,但我不知道如何让它解析数据中的动态字段。以下是查询返回的JSON数据示例:{-30655845:{id:"30655845"name:"testdatadescription:""latitude:"38"longitude:"-122"altitude:"0"thumbnailURL:http://someimage.com/url.jpgdistance:9566.6344386665}-28688744:{id:"28688744"name:"testdata2"description:""latitude:"38"longitude